package com.wondersgroup.insurance.dnuilearn.recyclerview.swipe;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wondersgroup.insurance.dnuilearn.R;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/6/28.
 */

public class QQMessageAdapter1 extends RecyclerView.Adapter<QQMessageAdapter1.MyViewHolder> implements  ItemTouchHelperAdapterCallback{
    private List<QQMessage> list;

    public QQMessageAdapter1(List<QQMessage> list) {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext().getApplicationContext()).inflate(R.layout.list_item_main,parent,false));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder,final int position) {
       final QQMessage qqMessage = list.get(position);
        holder.iv_logo.setImageResource(qqMessage.getLogo());
        holder.tv_name.setText(qqMessage.getName());
        holder.tv_lastMsg.setText(qqMessage.getLastMsg());
        holder.tv_time.setText(qqMessage.getTime());


//        holder.iv_logo.setOnTouchListener(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                // 传递事件
//                if(event.getAction()==MotionEvent.ACTION_DOWN){
////					ItemTouchHelper.startDrag();
////
//                    Log.e(""," position = "+position + "  qqMessage = "+qqMessage.getName());
//                }
//                return false;
//            }
//        });

        holder.mActionViewRefresh.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(view.getContext(), "Refresh Click" + holder.getAdapterPosition()
                                , Toast.LENGTH_SHORT).show();
                    }
                }

        );
        holder.mActionViewDelete.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.d("", "mActionViewDelete");
                        doDelete(holder.getAdapterPosition());
                    }
                }

        );
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }

    @Override
    public void onItemSwipe(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
       public ImageView iv_logo;
       public TextView tv_name;
       public TextView tv_time;
       public TextView tv_lastMsg;
        public LinearLayout content;
        public LinearLayout   view_list_repo_action_container;
        View mActionViewDelete;
        View mActionViewRefresh;
       public MyViewHolder(View itemView) {
           super(itemView);
           iv_logo = (ImageView) itemView.findViewById(R.id.iv_logo);
           tv_name = (TextView)itemView.findViewById(R.id.tv_name);
           tv_time = (TextView)itemView.findViewById(R.id.tv_time);
           tv_lastMsg = (TextView)itemView.findViewById(R.id.tv_lastMsg);
           content = (LinearLayout)itemView.findViewById(R.id.content);
           view_list_repo_action_container = (LinearLayout)itemView.findViewById(R.id.view_list_repo_action_container);
           mActionViewDelete = itemView.findViewById(R.id.view_list_repo_action_delete);
           mActionViewRefresh = itemView.findViewById(R.id.view_list_repo_action_update);
       }
   }

    private void doDelete(int adapterPosition) {
        list.remove(adapterPosition);
        notifyItemRemoved(adapterPosition);
    }
}
